#include "widget_funcb_smart_vision.h"
#include "ui_widget_funcb_smart_vision.h"
#include <QMessageBox>

Widget_funcB_Smart_vision::Widget_funcB_Smart_vision(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcB_Smart_vision)
{
    ui->setupUi(this);
    // 设置底部按钮文本
    ui->pushButton->setText("打开存储文件夹");
    this->ui->close_putton->setText("关闭摄像头");
    this->ui->open_button->setText("打开摄像头");
    this->ui->tack_pcture_button->setText("拍照");
    // 初始化保存路径
    savePath = "./save_images";
    // 确保保存文件夹存在
    QDir dir(savePath);
    if (!dir.exists())
    {
        dir.mkpath(savePath);
    }
    // 初始化摄像头相关对象，先置空，在打开摄像头时创建
    camera = nullptr;
    viewfinder = nullptr;
    imageCapture = nullptr;
}

Widget_funcB_Smart_vision::~Widget_funcB_Smart_vision()
{
    // 释放摄像头相关资源
    if (camera)
    {
        camera->stop();
        delete camera;
    }
    if (viewfinder)
    {
        delete viewfinder;
    }
    if (imageCapture)
    {
        delete imageCapture;
    }
    delete ui;
}

// 打开存储文件夹
void Widget_funcB_Smart_vision::on_pushButton_clicked()
{
    QDir folder(savePath);
    if (!folder.exists())
    {
        folder.mkpath(savePath);
    }
    QUrl folderUrl = QUrl::fromLocalFile(savePath);
    QDesktopServices::openUrl(folderUrl);
}

// 图像保存完成的回调
void Widget_funcB_Smart_vision::imageSaved(int id, const QString &fileName)
{
    Q_UNUSED(id);
    QMessageBox::information(this, "提示", "照片已保存至：" + fileName);
}

// 拍照
void Widget_funcB_Smart_vision::on_tack_pcture_button_clicked()
{
    if (imageCapture)
    {
        // 设置照片保存路径和文件名，这里用当前时间作为文件名，格式为.jpg
        QString fileName = savePath + "/capture_" + QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss") + ".jpg";
        // 进行抓拍，id 为捕获的图像标识，这里暂时不使用
        int id = imageCapture->capture(fileName);
    } else
    {
        QMessageBox::warning(this, "警告", "请先打开摄像头！");
    }
}

// 关闭摄像头
void Widget_funcB_Smart_vision::on_close_putton_clicked()
{
    if (camera)
    {
        camera->stop();
        delete camera;
        camera = nullptr;
    }
    if (viewfinder)
    {
        viewfinder->hide();
        delete viewfinder;
        viewfinder = nullptr;
    }
    if (imageCapture)
    {
        delete imageCapture;
        imageCapture = nullptr;
    }
}

// 打开摄像头
void Widget_funcB_Smart_vision::on_open_button_clicked()
{
    // 如果摄像头已存在，先停止并释放
    if (camera)
    {
        camera->stop();
        delete camera;
        camera = nullptr;
    }
    if (viewfinder)
    {
        delete viewfinder;
        viewfinder = nullptr;
    }

    if (imageCapture)
    {
        delete imageCapture;
        imageCapture = nullptr;
    }

    // 创建摄像头对象，默认使用第一个可用摄像头
    camera = new QCamera(this);
    // 创建取景器，用于在中间窗口显示摄像头画面
    viewfinder = new QCameraViewfinder(ui->widget_Img);
    viewfinder->setGeometry(ui->widget_Img->geometry());
    camera->setViewfinder(viewfinder);
    // 创建图像捕获对象，用于抓拍
    imageCapture = new QCameraImageCapture(camera, this);
    // 连接图像保存完成的信号到槽函数，以便反馈
    connect(imageCapture, &QCameraImageCapture::imageSaved, this, &Widget_funcB_Smart_vision::imageSaved);
    // 设置图像保存格式和编码等
    imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);

    // 启动摄像头
    camera->start();
    viewfinder->show();
}
